package ch.bfh.mobicomp.soda2.gps_datacollector.presentation;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import ch.bfh.mobicomp.soda2.gps_datacollector.R;
import ch.bfh.mobicomp.soda2.gps_datacollector.business.SODA_GPS_DataCollector_Service;
import ch.bfh.mobicomp.soda2.gps_datacollector.business.listener.SODA_GPS_LocationListener;

public class SODA_GPS_MainActivity extends Activity {

    private static final String TAG = SODA_GPS_MainActivity.class.getName();

    private Intent intent = null;
    private static int counter = 0;
    private static TextView textViewCounter;
    private static TextView textViewLongitude;
    private static TextView textViewLatitude;
    private static TextView textViewAltitude;
    private static TextView textViewtime;

    public static final String UI_BROADCAST = "GPS_UI_UPDATE_BROADCAST";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textViewCounter = (TextView) findViewById(R.id.tv_main_count_data);
        textViewLatitude = (TextView) findViewById(R.id.tv_main_latitude_data);
        textViewLongitude = (TextView) findViewById(R.id.tv_main_longitude_data);
        textViewAltitude = (TextView) findViewById(R.id.tv_main_altitude_data);
        textViewtime = (TextView) findViewById(R.id.tv_main_timestamp_data);
        textViewCounter.setText(counter + "");
        intent = new Intent(this, SODA_GPS_DataCollector_Service.class);
    }

    public void onButtonClick(View view) {
        Button start = (Button) findViewById(R.id.bu_activity_gps_main_service_start);
        Button start2 = (Button) findViewById(R.id.bu_activity_gps_main_service_start2);
        switch (view.getId()) {
        case R.id.bu_activity_gps_main_service_start:
            intent = SODA_GPS_DataCollector_Service.getStartGPSServicetIntent(0.001f, 500l);
            sendBroadcast(intent);
            Toast.makeText(this, "Start ON THE FLY", Toast.LENGTH_SHORT).show();
            view.setEnabled(false);
            start2.setEnabled(false);
            break;
        case R.id.bu_activity_gps_main_service_stop:
            intent = SODA_GPS_DataCollector_Service.getStopGPSServiceIntent();
            sendBroadcast(intent);
            Toast.makeText(this, "Stop", Toast.LENGTH_SHORT).show();
            counter = 0;
            start.setEnabled(true);
            start2.setEnabled(true);
            break;
        case R.id.bu_activity_gps_main_service_start2:
            intent = SODA_GPS_DataCollector_Service.getStartGPSServicetIntent(0.001f, 3000l);
            sendBroadcast(intent);
            Toast.makeText(this, "Start FIND MY", Toast.LENGTH_SHORT).show();
            view.setEnabled(false);
            start.setEnabled(false);
            break;
        }
    }

    public static class LocationReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "Broadcast Received: ");
            if (SODA_GPS_MainActivity.textViewCounter != null && SODA_GPS_MainActivity.textViewLatitude != null
                    && SODA_GPS_MainActivity.textViewLongitude != null
                    && SODA_GPS_MainActivity.textViewAltitude != null && SODA_GPS_MainActivity.textViewtime != null) {

                if (intent != null) {
                    if (intent.hasExtra("MESSAGE")) {
                        SODA_GPS_MainActivity.textViewCounter.setText(intent.getStringExtra("MESSAGE"));
                    }
                    if (intent.hasExtra(SODA_GPS_LocationListener.LATITUDE)
                            && intent.hasExtra(SODA_GPS_LocationListener.LONGITUDE)) {
                        Log.d(TAG, "Latitude: " + intent.getStringExtra(SODA_GPS_LocationListener.LATITUDE));
                        Log.d(TAG, "LONGITUDE: " + intent.getStringExtra(SODA_GPS_LocationListener.LONGITUDE));
                        Log.d(TAG, "Altitude: " + intent.getStringExtra(SODA_GPS_LocationListener.ALTITUDE));
                        Log.d(TAG, "Altitude: " + intent.getStringExtra(SODA_GPS_LocationListener.TIME));
                        Log.d(TAG, "Counter: " + ++counter);
                        SODA_GPS_MainActivity.textViewCounter.setText(counter + "");
                        SODA_GPS_MainActivity.textViewLatitude.setText(intent
                                .getStringExtra(SODA_GPS_LocationListener.LATITUDE));
                        SODA_GPS_MainActivity.textViewLongitude.setText(intent
                                .getStringExtra(SODA_GPS_LocationListener.LONGITUDE));
                        SODA_GPS_MainActivity.textViewAltitude.setText(intent
                                .getStringExtra(SODA_GPS_LocationListener.ALTITUDE));
                        SODA_GPS_MainActivity.textViewtime.setText(intent
                                .getStringExtra(SODA_GPS_LocationListener.TIME));
                    }

                }
            }
        }
    }

    @Override
    protected void onPause() {
        // intent = new Intent(this, GPS_DataCollector_Service.class);
        stopService(intent);
        Toast.makeText(this, "SODA2 GPS_SERVICE stoped", Toast.LENGTH_SHORT).show();
        counter = 0;
        super.onPause();
    }
}
